home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / install-memo.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  3KB  |  139 lines

  1. /* install-memo.c:  Pilot memo pad installer
  2.  *
  3.  * This is free software, licensed under the GNU Public License V2.
  4.  * See the file COPYING for details.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "pi-source.h"
  10. #include "pi-socket.h"
  11. #include "pi-dlp.h"
  12. #include "pi-memo.h"
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.   struct pi_sockaddr addr;
  17.   int db;
  18.   int sd;
  19.   int i;
  20.   int l;
  21.   int memo_size;
  22.   char *memo_buf;
  23.   FILE *f;
  24.   struct PilotUser U;
  25.   int ret;
  26.   char buf[0xffff];
  27.   int category;
  28.   struct MemoAppInfo mai;
  29.  
  30.   if (argc < 3) {
  31.     fprintf(stderr,"usage:%s %s [-c category] file [file] ...\n",argv[0],TTYPrompt);
  32.     exit(2);
  33.   }
  34.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  35.     perror("pi_socket");
  36.     exit(1);
  37.   }
  38.     
  39.   addr.pi_family = PI_AF_SLP;
  40.   strcpy(addr.pi_device,argv[1]);
  41.   
  42.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  43.   if(ret == -1) {
  44.     perror("pi_bind");
  45.     exit(1);
  46.   }
  47.  
  48.   ret = pi_listen(sd,1);
  49.   if(ret == -1) {
  50.     perror("pi_listen");
  51.     exit(1);
  52.   }
  53.  
  54.   sd = pi_accept(sd, 0, 0);
  55.   if(sd == -1) {
  56.     perror("pi_accept");
  57.     exit(1);
  58.   }
  59.  
  60.   /* Ask the pilot who it is. */
  61.   dlp_ReadUserInfo(sd,&U);
  62.   
  63.   /* Tell user (via Pilot) that we are starting things up */
  64.   dlp_OpenConduit(sd);
  65.   
  66.   /* Open the Memo Pad's database, store access handle in db */
  67.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "MemoDB", &db) < 0) {
  68.     puts("Unable to open MemoDB");
  69.     dlp_AddSyncLogEntry(sd, "Unable to open MemoDB.\n");
  70.     exit(1);
  71.   }
  72.   
  73.   l = dlp_ReadAppBlock(sd, db, 0, (unsigned char *)buf, 0xffff);
  74.   unpack_MemoAppInfo(&mai, (unsigned char *)buf, l);
  75.  
  76.   category = 0;
  77.   
  78.   for (i=2; i<argc; i++) {
  79.   
  80.     if (strcmp(argv[i],"-c")==0) {
  81.       for (l=0; l<16; l++)
  82.         if (strcasecmp(mai.category.name[l], argv[i+1]) == 0) {
  83.           category = l;
  84.           break;
  85.         }
  86.       if (l==16)
  87.         category = atoi(argv[i+1]);
  88.       i++;
  89.       continue;
  90.     }
  91.  
  92.     f = fopen(argv[i], "r");
  93.     if (f == NULL) {
  94.       perror("fopen");
  95.       exit(1);
  96.     }
  97.  
  98.     fseek(f, 0, SEEK_END);
  99.     memo_size = ftell(f);
  100.     fseek(f, 0, SEEK_SET);
  101.  
  102.     l = strlen(argv[i]);
  103.  
  104.     memo_buf = (char*)malloc(memo_size + l + 2);
  105.     if (memo_buf == NULL) {
  106.       perror("malloc()");
  107.       exit(1);
  108.     }
  109.  
  110.     strcpy(memo_buf, argv[i]);
  111.     memo_buf[l] = '\n';
  112.  
  113.     fread(memo_buf + l + 1, memo_size, 1, f);
  114.     memo_buf[l + 1 + memo_size] = '\0';
  115.  
  116.     /* dlp_exec(sd, 0x26, 0x20, &db, 1, NULL, 0); */
  117.     dlp_WriteRecord(sd, (unsigned char)db, 0, 0, category,
  118.             (unsigned char *)memo_buf, -1, 0);
  119.     free(memo_buf);
  120.   }
  121.  
  122.   /* Close the database */
  123.   dlp_CloseDB(sd, db);
  124.  
  125.   /* Tell the user who it is, with a different PC id. */
  126.   U.lastSyncPC = 0x00010000;
  127.   U.successfulSyncDate = time(NULL);
  128.   U.lastSyncDate = U.successfulSyncDate;
  129.   dlp_WriteUserInfo(sd,&U);
  130.  
  131.   dlp_AddSyncLogEntry(sd, "Wrote memo to Pilot.\n");
  132.   
  133.   /* All of the following code is now unnecessary, but harmless */
  134.   
  135.   dlp_EndOfSync(sd,0);
  136.   pi_close(sd);
  137.   exit(0);
  138. }
  139.